package com.hxm.appGenealogy.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxm.appGenealogy.Function.DynamicFunction;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.entity.Dynamic;
import com.hxm.appGenealogy.entity.Like;
import com.hxm.appGenealogy.entity.People;
import com.hxm.appGenealogy.entity.ResponDataVo;
import com.hxm.appGenealogy.mapper.LikeMapper;
import com.hxm.appGenealogy.service.DynamicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("app/like")
public class LikeController {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private DynamicFunction dynamicFunction;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private PeopleFunction peopleFunction;

    /**
     * 点赞;
     * @param like
     * @return
     */
    @PostMapping("like")
    public ResponDataVo insert(@RequestBody Like like) {
        String parmAnalysis = ParmIntended.parmAnalysis(like, "peopleId", "dynamicId");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        Dynamic dynamic = dynamicFunction.selectByid(like.getDynamicId());
        dynamic.setPraise(dynamic.getPraise()+1);
        dynamicFunction.updateByid(dynamic);
        like.setTime(new Date());
        likeMapper.insert(like);
        return new ResponDataVo(true);
    }

    /**
     * 查询我给谁点了赞;
     * @return
     */
    @PostMapping("selectpeopledynamic")
    public ResponDataVo selectPeopleDynamic(@RequestBody People people){
        String parmAnalysis = ParmIntended.parmAnalysis(people, "id");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        /*People people2 = peopleFunction.peopleGetByid(people.getId());*/
        LambdaQueryWrapper<Like> likeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeLambdaQueryWrapper.eq(Like::getPeopleId,people.getId()).orderByDesc(Like::getTime);
        List<Like> likes = likeMapper.selectList(likeLambdaQueryWrapper);
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (Like like : likes) {
            Long dynamicId = like.getDynamicId();
            Dynamic dynamic = dynamicFunction.selectByid(dynamicId);
            Long peopleId = like.getPeopleId();
            People people1 = peopleFunction.peopleGetByid(peopleId);
            Map<String, Object> stringObjectMap = dynamicService.stringObjectMap(dynamic, people1,people.getId());
            mapList.add(stringObjectMap);
        }
        return new ResponDataVo(mapList);
    }
    /**
     * 取消点赞;
     * @param like
     * @return
     */
    @PostMapping("delLike")
    public ResponDataVo delLike(@RequestBody Like like){
        String parmAnalysis = ParmIntended.parmAnalysis(like, "peopleId", "dynamicId");
        if(parmAnalysis!=null){
            return new ResponDataVo(parmAnalysis);
        }
        LambdaQueryWrapper<Like> likeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        likeLambdaQueryWrapper.eq(Like::getDynamicId,like.getDynamicId()).eq(Like::getPeopleId,like.getPeopleId());
        Like like1 = likeMapper.selectOne(likeLambdaQueryWrapper);
        //删除当前点赞的数据;
        likeMapper.deleteById(like1.getId());
        //当前作品的点赞的人数进行-1;
        Dynamic dynamic = dynamicFunction.selectByid(like.getDynamicId());
        dynamic.setPraise(dynamic.getPraise()-1);
        dynamicFunction.updateByid(dynamic);
        return new ResponDataVo(true);
    }
}







